<?php
namespace Api\Controller;

use Think\Controller;
use Base\FrontController;

/**
 * @name 前端日历相关功能提交异步数据到数据库 ，访问本接口下任何方法均要求session中已保留注册用户信息
 * swagger-ui api文档生成cmd： php D:\work\php\swagger-php\vendor\zircote\swagger-php\bin\swagger D:\work\php\wx-third-platform\code\Application\Api\Controller\swagger D:\work\php\wx-third-platform\code\Application\Api\Controller\ApiBHCalendarController.class.php  -o D:\work\php\wx-third-platform\code\swui\日历-calendar.json
 * @author perry
 *
 */
class ApiBHCalendarController extends FrontController
{

    // private $weixin_user;

    function _initialize()
    {
        parent::_initialize();
        
        $this->calendarM=D('Home/BHCalendarView');
        $this->calendarSM=D('Home/BHCalendar');
        $this->looksM=D('Home/BHLooks');
        $this->uid=$this->weixin_user['id'];
    }

     /**
     * @SWG\Get(
     *     path="/ApiBHCalendar/getMonthCalendar",
     *     tags={"getMonthCalendar"},
     *     description="传入年月获得当前登录用户该月每日造型列表",
     *     operationId="getMonthCalendar",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="year",
     *         in="query",
     *         description="指定年",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="month",
     *         in="query",
     *         description="指定月",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="当前登录用户的指定月每日造型列表",
     *         @SWG\Schema(
     *             ref="#/definitions/calendars"
     *         ),
     *     ),
     *     @SWG\Response(
     *         response="default",
     *         description="接口调用出错",
     *         @SWG\Schema(
     *             ref="#/definitions/ErrorModel"
     *         )
     *     )
     * )
     */
    public function getMonthCalendar(){
        $month=I('month');
        $year=I('year');
        $re=$this->calendarM->getMonthCalendar($year,$month,$this->uid);
        // dump($re);
        $this->customReturn(1,$re);
        
    }

    /**
     * @SWG\Get(
     *     path="/ApiBHCalendar/setLooksToCalendar",
     *     tags={"setLooksToCalendar"},
     *     description="将指定造型加入日历中的指定日",
     *     operationId="setLooksToCalendar",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="year",
     *         in="query",
     *         description="指定年(范例：2017)",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="month",
     *         in="query",
     *         description="指定月(范例：8)",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="day",
     *         in="query",
     *         description="指定日(范例：8)",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="look_id",
     *         in="query",
     *         description="指定造型主键编号",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="is_top",
     *         in="query",
     *         description="是否以icon形式显示在月日历视图,0否1是，默认1",
     *         required=false,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="添加到指定日的造型详情信息",
     *         @SWG\Schema(
     *             ref="#/definitions/look"
     *         ),
     *     ),
     *     @SWG\Response(
     *         response="default",
     *         description="接口调用出错",
     *         @SWG\Schema(
     *             ref="#/definitions/ErrorModel"
     *         )
     *     )
     * )
     */
    public function setLooksToCalendar(){
        $year=I('year');
        if(!ic_isPosInt($year)){
            throw new \Exception("指定年无效,请修改", -215);
        }
        $month=I('month');
        if(!ic_isPosInt($month)){
            throw new \Exception("指定月无效,请修改", -216);
        }
        $day=I('day');
        if(!ic_isPosInt($day)){
            throw new \Exception("指定日无效,请修改", -217);
        }
        $look_id=I('look_id');
        if(!ic_isPosInt($look_id)){
            throw new \Exception("指定造型无效,请修改", -218);
        }
        $looks=$this->looksM->where('id='.$look_id)->find();
        if(!$looks){
            throw new \Exception("待添加到日历的造型不存在，请检查", -219);
        }
        $looks['img']=ic_getUploadImgFullPath('looks',$looks['img'],$looks['create_time']);
        $is_top=I('is_top',1);
        $is_top+=1;
        if(!in_array($is_top, array(1,2))){
            throw new \Exception("是否显示在月日历页参数无效，请检查", -220);
        }
        $is_top-=1;
        $newData=array();
        $newData['uid']=$this->uid;
        $newData['year']=$year;
        $newData['month']=$month;
        $newData['day']=$day;

        //乐佳超要求每个指定日只能添加一个造型，这里先判断指定日是否有造型
        $exists=$this->calendarSM->field('id,look_id,is_top')->where($newData)->select();
        $alreadyExist=false;
        foreach ($exists as $key => $exist) {
            if($exist['look_id']==$look_id){
                if($exist['is_top']!=$is_top){
                    $udata=array('update_time'=>time(),'is_top'=>$is_top);
                    $this->calendarSM->where('id='.$exist['id'])->save($udata);
                }
                $alreadyExist=true;
            }
            else {
                // if($is_top==1){
                //     if($exist['is_top']==1){
                //         $udata=array('update_time'=>time(),'is_top'=>0);
                //         $this->calendarSM->where('id='.$exist['id'])->save($udata);
                //     }
                // }
                //由于每个指定日只能添加一个造型，该日其它造型只能删掉
                $this->calendarSM->where('id='.$exist['id'])->delete();
            }
        }
        if(!$alreadyExist){
            $newData['look_id']=$look_id;
            $newData['memo']='';
            $newData['create_time']=time();
            $newData['update_time']=$newData['create_time'];
            $newData['status']=1;
            $newData['is_top']=$is_top;
            $newId=$this->calendarSM->add($newData);
            if(!ic_isPosInt($newId)){
                throw new \Exception("添加失败，请稍后再试或联系管理员", -221);
            }
        }
        $this->customReturn(1,$looks);
        
    }

    /**
     * @SWG\Get(
     *     path="/ApiBHCalendar/getRecommendLooksbyDay",
     *     tags={"getRecommendLooksbyDay"},
     *     description="获得指定日的百家好推荐造型",
     *     operationId="getRecommendLooksbyDay",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="year",
     *         in="query",
     *         description="指定年(范例：2017)",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="month",
     *         in="query",
     *         description="指定月(范例：8)",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="day",
     *         in="query",
     *         description="指定日(范例：8)",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="或得到的指定日百家好推荐造型详情信息",
     *         @SWG\Schema(
     *             ref="#/definitions/look"
     *         ),
     *     ),
     *     @SWG\Response(
     *         response="default",
     *         description="接口调用出错",
     *         @SWG\Schema(
     *             ref="#/definitions/ErrorModel"
     *         )
     *     )
     * )
     */
    public function getRecommendLooksbyDay(){
        $year=I('year');
        if(!ic_isPosInt($year)){
            throw new \Exception("指定年无效,请修改", -215);
        }
        $month=I('month');
        if(!ic_isPosInt($month)){
            throw new \Exception("指定月无效,请修改", -216);
        }
        $day=I('day');
        if(!ic_isPosInt($day)){
            throw new \Exception("指定日无效,请修改", -217);
        }
        $map=array();
        $map['uid']=array('in',C('super_style_uid'));
        $map['year']=$year;
        $map['month']=$month;
        $map['day']=$day;
        $map['is_top']=1;
        $exist=$this->calendarSM->where($map)->find();
        $looks=false;
        if($exist){
            $looks=$this->looksM->where('id='.$exist['look_id'])->find();
            if($looks){
                $looks['img']=ic_getUploadImgFullPath('looks',$looks['img'],$looks['create_time']);
            }
            
        }
        $this->customReturn(1,$looks);
    }



    // /**
    //  * 上传图片文件到/Uploads/nail_photos/tmp/目录下
    //  * 如果有旧文件要删除,通过I('osn')传递进来
    //  */
    // public function cupload()
    // {
    //     try {
    //         $upload = new \Think\Upload();// 实例化上传类
    //         $upload->maxSize = 1457280;// 设置附件上传大小
    //         $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    //         $upload->rootPath = $this->uploadRootPath; // 设置附件上传根目
    //         //子目录创建方式
    //         $upload->subName = $this->uploadSubTmpPath;
    //         $re = $upload->upload();
    //         $err = $upload->getError();
    //         if ($err) {
    //             throw new \Exception($err, -1);
    //         }
    //         //判断是否有旧文件要删除
    //         $old_file = I('osn');
    //         if ($old_file) {
    //             $filepath = $upload->rootPath . $upload->subName . $old_file;
    //             unlink($filepath);
    //         }
    //         $info['savename'] = $re['imagefile']['savename'];
    //         $this->customReturn(1, array('sn' => $re['imagefile']['savename']));
    //     } catch (\Exception $ecx) {
    //         $this->customReturn(-1, array('errmsg' => $ecx->getMessage(), 'errcode' => $redirect_url));
    //     }
    // }

    // /**
    //  * 转换session
    //  * @param 名称 $key
    //  * @param 值 $value
    //  */
    // private function changeSession($key, $value)
    // {
    //     $_SESSION[$key] = $value;
    //     session($key, $value);
    // }


    

}
